﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Primary Key &amp; Identity Fields | DotNetHelper.ObjectToSql </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Primary Key &amp; Identity Fields | DotNetHelper.ObjectToSql ">
    <meta name="generator" content="docfx 2.43.1.0">
    
    <link rel="shortcut icon" href="../../logo/favicon.ico">
    <link rel="stylesheet" href="../../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../../styles/docfx.css">
    <link rel="stylesheet" href="../../styles/main.css">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    <meta property="docfx:navrel" content="toc.html">
    <meta property="docfx:tocrel" content="../toc.html">
    
    <meta property="docfx:rel" content="../../">
    
  </head>  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../../index.html">
                <img id="logo" class="svg" src="../../logo.svg" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
                
                <ul class="nav level1 navbar-nav">
                      <li>
                          <a href="../../index.html" title="Home">Home</a>
                      </li>
                      <li>
                          <a href="../index.html" title="Tutorials">Tutorials</a>
                      </li>
                      <li>
                          <a href="../../api/DotNetHelper.ObjectToSql.Attribute.html" title="API">API</a>
                      </li>
                      <li>
                          <a href="https://github.com/TheMofaDe/DotNetHelper.ObjectToSql/blob/master/CHANGELOG.md" title="Change Log">Change Log</a>
                      </li>
                      <li>
                          <a href="https://github.com/TheMofaDe/DotNetHelper.ObjectToSql" title="Github">Github</a>
                      </li>
                </ul>    </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div class="container body-content">
        
        <div id="search-results">
          <div class="search-list"></div>
          <div class="sr-items">
            <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
          </div>
          <ul id="pagination"></ul>
        </div>
      </div>
      <div role="main" class="container body-content hide-when-search">
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div>
              <div class="sidefilter">
                <form class="toc-filter">
                  <span class="glyphicon glyphicon-filter filter-icon"></span>
                  <input type="text" id="toc_filter_input" placeholder="Enter here to filter..." onkeypress="if(event.keyCode==13) {return false;}">
                </form>
              </div>
              <div class="sidetoc">
                <div class="toc" id="toc">
                  
                  <ul class="nav level1">
                    <li class="">
                      <a href="../index.html" title="Home" class="">Home</a>
                    </li>
                    <li class="">
                      <a href="../Attributes/attribute.html" title="Attributes" class="">Attributes</a>
                    </li>
                    <li class="">
                      <span class="expand-stub"></span>
                      <a class="">How to Generate SQL</a>
                        
                        <ul class="nav level2">
                          <li class="active">
                            <a href="../GenerateSql/key.html" title="How-to1" class="active">How-to1</a>
                          </li>
                          <li class="">
                            <a href="../GenerateSql/test.html" title="How-to2" class="">How-to2</a>
                          </li>
                        </ul>  </li>
                  </ul>        </div>
              </div>
            </div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="Tutorial.GenerateSQL">
<h1 id="primary-key--identity-fields">Primary Key &amp; Identity Fields</h1>

<p>In the secnarios where you need to build Update,Delete, or Upsert Statements. Attributes are use to generate the where clause.</p>
<h3 id="decorating-properties-as-primary-key">Decorating Properties As Primary Key</h3>
<pre><code class="lang-csharp">// USING SqlColumn Attribute
public class Employee {
      [SqlColumn(SetPrimaryKey = true)]
      public PrimaryKey {get; set;}
      public FirstName { get; set; }
      public LastName  { get; set; }
}
</code></pre>
<p>OR</p>
<pre><code class="lang-csharp">// USING DataAnnotation Attribute
public class Employee {
      [Key]
      public PrimaryKey {get; set;}
      public FirstName { get; set; }
      public LastName  { get; set; }
}
</code></pre>
<p>using either version of the model above I can now generate update,delete, &amp; upsert statment by doing the following</p>
<pre><code class="lang-csharp">   var actionType = ActionType.Update; // A enum with the values Insert,Update,Delete,Upsert
   var sqlServerObjectToSql = new ObjectToSql(DataBaseType.SqlServer);
   var updateSql = sqlServerObjectToSql.BuildQuery&lt;Employee&gt;(&quot;Table Name Go Here If Null Defaults to Type Name&quot;,actionType);
   var upsertSql = sqlServerObjectToSql.BuildQuery&lt;Employee&gt;(&quot;Table Name Go Here If Null Defaults to Type Name&quot;,ActionType.Upsert);
   var deleteSql = sqlServerObjectToSql.BuildQuery&lt;Employee&gt;(&quot;Table Name Go Here If Null Defaults to Type Name&quot;,ActionType.Delete);

   Console.WriteLine(updateSql);
   Console.WriteLine(upsertSql);
   Console.WriteLine(deleteSql);
</code></pre>
<p>running the code above will produces the following sql statmente</p>
<pre><code class="lang-sql">UPDATE Employee SET [FirstName]=@FirstName,[LastName]=@LastName WHERE [PrimaryKey]=@PrimaryKey
</code></pre>
<pre><code class="lang-sql">UPDATE Employee SET [FirstName]=@FirstName,[LastName]=@LastName WHERE [PrimaryKey]=@PrimaryKey
</code></pre>
<pre><code class="lang-sql">DELETE FROM EmployeeWithPrimaryKeySqlColumn WHERE [PrimaryKey]=@PrimaryKey
</code></pre>
<div class="WARNING">
<h5>Warning</h5>
<p>Executing the a update,upsert, or delete query with a type that doesn't have any key attributes with lead to an InvalidOperationException being thrown</p>
</div>
<!-- ### Supported Attributes 
this library has its own custom attributes and can also work with the common DataAnnotation attributes. With the support of DataAnnotation this means this library could be paired with your favorite orm like Dapper or Enitity Framework -->
<h1 id="custom-attributes">Custom Attributes</h1>
<p>In the secnarios where you need to build Update,Delete, or Upsert Statements. Attributes are use to generate the where clause. This library has its own custom attributes and can also work with the common DataAnnotation attributes. With the support of DataAnnotation this means this library could be paired with your favorite orm like Dapper or Enitity Framework</p>
<h6 id="mark-a-property-as-an-identity-fields">Mark a property as an identity fields.</h6>
<pre><code class="lang-csharp">[SqlColumn(SetIsIdentityKey = true)]
OR 
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
</code></pre>
<h6 id="mark-a-property-as-a-key-field">Mark a property as a key field.</h6>
<pre><code class="lang-csharp">[SqlColumn(SetIsIdentityKey = true)]
OR 
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
</code></pre>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                  <li>
                    <a href="https://github.com/TheMofaDe/DotNetHelper.ObjectToSql/blob/master/docs/tutorials/GenerateSql/key.md/#L1" class="contribution-link">Improve this Doc</a>
                  </li>
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
              <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            Copyright © 2019  <strong>Joseph McNeal Jr</strong>
            
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../../styles/docfx.js"></script>
    <script type="text/javascript" src="../../styles/main.js"></script>
  </body>
</html>
